package com.nlx.notes.module.ai.tools;

import com.nlx.notes.module.bean.vo.CategoryVO;
import com.nlx.notes.module.core.util.JSONUtils;
import com.nlx.notes.module.entity.Categories;
import com.nlx.notes.module.service.FileReaderService;
import com.nlx.notes.module.service.ICategoriesService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.List;

/**
 * 流水分类工具
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class CategoriesTool {
    /**
     * 分类列表工具提示词相对地址
     */
    private static final String GET_CATEGORIES_LIST_PROMPT = "tools/categories.md";

    private final FileReaderService readerService;
    private final ICategoriesService categoriesService;


    @Tool(name = "get_categories_list",description = "根据用户的ID查询用户的账单或者流水分类类型")
    public String getCategoriesList(@ToolParam(description = "用户的ID,必须填") Long userId) {
        log.info("用户查询分类列表，入参：{}", userId);
        List<CategoryVO> categoryVOS = categoriesService.categoryList(null,userId);
        String prompt = readerService.readFile(GET_CATEGORIES_LIST_PROMPT);
        return MessageFormat.format(prompt,JSONUtils.toString(categoryVOS));
    }

    @Tool(name = "add_categories",description = "添加用户的账单或者流水分类类型")
    public String addCategories(@ToolParam(description = "用户的ID,必须填") Long userId,
                                    @ToolParam(description = "分类的名称,必须填") String name,
                                    @ToolParam(description = "分类的父级ID,必须填,如果当前就是父类请填0") Long parentId) {
        log.info("用户添加分类，入参：{},{},{}", userId,name,parentId);
        Categories categories = new Categories();
        categories.setUserId(userId);
        categories.setName(name);
        categories.setParentId(parentId);
        if (categoriesService.save(categories)){
            String prompt = readerService.readFile(GET_CATEGORIES_LIST_PROMPT);
            return MessageFormat.format(prompt,JSONUtils.toString(categoriesService.categoryList(null,userId)));
        }
        return "添加失败了。";
    }

}
